package edu.drexel.cs575.phw1.scale;

import java.util.ArrayList;
import java.util.List;

import listeners.PowerLevelListener;

/**
 * Power to run the scale.
 * 
 * @author anthony
 *
 */
public class Power {
	private int level;
	private List<PowerLevelListener> listeners = new ArrayList<PowerLevelListener>();

	public Power(int level) {
		setPowerLevel(level);
	}
	
	public void setPowerLevel(int level) {
		if (this.level < 0) {
			throw new IllegalArgumentException("Power level cannot be less than zero");
		}
		
		this.level = level;
		
		notifyLevelChangedListeners();
	}
	
	public int getPowerLevel() {
		return this.level;
	}
	
	public void addListener(PowerLevelListener listener) {
		if (listener == null) {
			throw new IllegalArgumentException("Listener cannot be null");
		}
		
		this.listeners.add(listener);
	}
	
	private void notifyLevelChangedListeners() {
		for(PowerLevelListener listener : listeners) {
			listener.powerLevelChanged(this);
		}
	}
}
